package com.rykj.qxj.test.xtest.httpclient;

import static org.hamcrest.CoreMatchers.containsString;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletRequest;

public class RequestUrlDmo {

	public static void main(String[] args) {
		
		String url="http://172.22.1.175/di/grid.action?userId=gmcrsz&pwd=szmb1601&interfaceId=intGetData2D&modelid=ec_ensemble";
		
		String url1="/data-api/outSide/getecensembleinfomation";
		Map<String, Object> paramMap=new HashMap<String,Object>();
		paramMap.put("name", "name");
		paramMap.put("pwd", "pwd");
		paramMap.put("aa", "aa");
		paramMap.put("bb", "bb");
		 Set<Entry<String, Object>> entrySet = paramMap.entrySet();
		 for (Entry<String, Object> entry : entrySet) {
			 System.out.println("key="+entry.getKey()+" value="+entry.getValue());
		}
		//循环map
		String urlStrParamsByMap1 = getUrlStrParamsByMap1(url1,paramMap);
		System.out.println(urlStrParamsByMap1);
		/*Map<String, Object> paramMap=new HashMap<String,Object>();
		paramMap.put("level", "1000");
		paramMap.put("dataFormat", "xml2");
		paramMap.put("leadtime", "024");
		paramMap.put("appKey", "58dfd3f1eefd2f90747d2aab531fec4f");
		paramMap.put("starttime", "2020-02-09 20:00:00");
		paramMap.put("element", "fuse");
		String urlStrParamsByMap = getUrlStrParamsByMap(url,paramMap);
		System.out.println(urlStrParamsByMap);*/
		/*
		String url1="http://172.22.1.175/di/grid.action?userId= ${gmcrsz}&pwd=${szmb1601}&interfaceId=${intGetDa}";
		String url2 = getUrl(url1);
		System.out.println(url2);
		List<Map<String, String>> urlRequestParams = getURLRequestParams(url1);
		System.out.println(urlRequestParams.toString());
		for (Map<String, String> map : urlRequestParams) {
			Set<String> keys = map.keySet();
			for (String key : keys) {
				System.out.println(key);
				System.out.println(map.get(key));
				
			}
			
		}*/
	}
	
	private static String getUrlStrParamsByMap1(String url,Map<String, Object> map) {
        if (com.rykj.qxj.server.common.utils.StringUtils.isEmpty(map)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        sb.append(url).append("?");
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            sb.append(entry.getKey() + "=${" + entry.getKey()+"}");
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }
	
	public static String getUrl(String strURL) {
		String strPage = null;
		String[] arrSplit = null;
		strURL = strURL.trim();
		if(strURL.indexOf("?")==-1) {
			return strURL;
		}
		arrSplit = strURL.split("[?]");
		if (strURL.length() > 0) {
			if (arrSplit.length > 1) {
				if (arrSplit[0] != null) {
					strPage = arrSplit[0];
				}
			}
		}
		return strPage;
	}
	
	public static  List<Map<String, String>> getURLRequestParams(String url) {
		List<Map<String, String>> params=new ArrayList<Map<String,String>>();
		String[] arrSplit = null;
		String strUrlParam = truncateUrl(url);
		if (strUrlParam == null) {
			return params;
		}
		arrSplit = strUrlParam.split("[&]");
		for (String strSplit : arrSplit) {
			Map<String, String> mapRequest = new HashMap<String, String>();
			String[] arrSplitEqual = null;
			arrSplitEqual = strSplit.split("[=]");
			// 解析出键值
			if (arrSplitEqual.length >1) {
				// 正确解析
				mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
				params.add(mapRequest);

			} else {
				if (arrSplitEqual[0] != "") {
					// 只有参数没有值，不加入
					mapRequest.put(arrSplitEqual[0], "");
					params.add(mapRequest);
				}
			}
		}
		return params;
	}

	/**
	 * 去掉url中的路径，留下请求参数部分
	 * 
	 * @param strURL
	 *            url地址
	 * @return url请求参数部分
	 */
	private static  String truncateUrl(String strURL) {
		String strAllParam = null;
		String[] arrSplit = null;

		strURL = strURL.trim().toLowerCase();

		arrSplit = strURL.split("[?]");
		if (strURL.length() > 1) {
			if (arrSplit.length > 1) {
				if (arrSplit[1] != null) {
					strAllParam = arrSplit[1];
				}
			}
		}

		return strAllParam;
	}

	
	private static  String getUrlStrParamsByMap(String url,Map<String, Object> map) {
        if (map.isEmpty()) {
            return  url;
        }
        StringBuffer sb = new StringBuffer();
        if(url.indexOf('?')==-1) {
        	sb.append(url).append("?");
        }else {
        	 sb.append(url).append("&");
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
        	sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }
	
	
	
	

}
